package com.cheer.vo;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.cheer.entity.Account;
import com.cheer.entity.Applicant;
import com.cheer.entity.Organization;
import com.cheer.enums.TitleEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.text.NumberFormat;

@Data
@Accessors(chain = true)
@ApiModel("集体信息展示")
public class ApplicantVO {

    private Integer registerStatus;

    private String applicantName;

    private Integer title;

    private String titleYear;

    private Boolean awarded;

    private String phoneAccount;

    private String backupAccount;

    private Integer reviewType;

    private Long joinReviewOrgId;

    private String joinReviewOrgName;

    private String contactPerson;

    private String contactPersonPhone;

    private String contactPersonPost;

    private String contactPersonBirth;

    private Integer contactPersonAge;

    private Integer personNumber;

    private Integer youthNumber;

    private String youthPercent;

    private String phone;

    private String email;

    private String address;

    private String remark;

    private String registerTime;

    private String orgName;

    private Long orgId;

    private DocumentVO checkDoc;

    private String usedName;

    @ApiModelProperty("申请升级是否展示审核类型")
    private Boolean isShowReviewType;

    @ApiModelProperty("是否可以申请省级文明号")
    private Boolean isAllowProvince;

    @ApiModelProperty("荣誉等级星星")
    private Integer starNum;


    public static ApplicantVO build(Account account, Applicant applicant, Organization organization, Organization joinOrg, String resourceUrl) {
        ApplicantVO applicantVO = new ApplicantVO();
        applicantVO.setRegisterStatus(applicant.getRegisterStatus());
        applicantVO.setApplicantName(applicant.getApplicantName());
        if (applicant.getTitle() == TitleEnum.NO.getType()) {
            applicantVO.setTitle(applicant.getRegisterTitle());
            if (applicant.getRegisterTitle() != TitleEnum.NO.getType()) {
                applicantVO.setTitleYear(applicant.getTitleYear());
            }
            applicantVO.setAwarded(false);
        } else {
            applicantVO.setTitle(applicant.getTitle());
            applicantVO.setTitleYear(applicant.getTitleYear());
            applicantVO.setAwarded(true);
        }
        applicantVO.setPhoneAccount(account.getPhone());
        applicantVO.setBackupAccount(account.getBackupAccount());
        applicantVO.setReviewType(applicant.getReviewType());
        if (joinOrg != null) {
            applicantVO.setJoinReviewOrgId(joinOrg.getId());
            applicantVO.setJoinReviewOrgName(joinOrg.getOrgName());
        }
        applicantVO.setUsedName(applicant.getUsedName());

        if(StrUtil.isNotBlank(applicant.getCheckDoc())){
            applicantVO.setCheckDoc(DocumentVO.build( StringUtils.substringAfterLast(applicant.getCheckDoc(), File.separator),applicant.getCheckDoc(), resourceUrl ));
        }
        applicantVO.setContactPerson(applicant.getContactPerson());
        applicantVO.setContactPersonPhone(applicant.getContactPersonPhone());
        applicantVO.setContactPersonPost(applicant.getContactPersonPost());
        applicantVO.setContactPersonBirth(DateUtil.format(applicant.getContactPersonBirth(), DatePattern.NORM_DATE_FORMAT));
        applicantVO.setContactPersonAge(DateUtil.ageOfNow(applicant.getContactPersonBirth()));
        applicantVO.setPersonNumber(applicant.getPersonNumber());
        applicantVO.setYouthNumber(applicant.getYouthNumber());
        NumberFormat nt = NumberFormat.getPercentInstance();
        applicantVO.setYouthPercent(nt.format(1.0 * applicant.getYouthNumber() /
                applicant.getPersonNumber()));
        applicantVO.setPhone(applicant.getPhone());
        applicantVO.setEmail(applicant.getEmail());
        applicantVO.setAddress(applicant.getAddress());
        applicantVO.setRemark(applicant.getRemark());
        applicantVO.setRegisterTime(DateUtil.formatDateTime(applicant.getCreateTime()));
        applicantVO.setOrgName(organization.getOrgName());
        applicantVO.setOrgId(organization.getId());
        applicantVO.setStarNum(applicant.getStarNum());
        return applicantVO;
    }

}
